package org.linlinjava.litemall.admin.task;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.core.task.Task;
import org.linlinjava.litemall.core.util.BeanUtil;
import org.linlinjava.litemall.db.domain.LitemallActivity;
import org.linlinjava.litemall.db.service.LitemallActivityService;

public class ActivityStartTask extends Task {
    private final Log logger = LogFactory.getLog(ActivityStartTask.class);
    private int activityId = -1;

    public ActivityStartTask(Integer activityId, long delayInMilliseconds) {
        super("ActivityStartTask-" + activityId, delayInMilliseconds);
        this.activityId = activityId;
    }

    @Override
    public void run() {
        logger.info("系统开始处理延时任务---活动开始---" + this.activityId);

        LitemallActivityService activityService = BeanUtil.getBean(LitemallActivityService.class);

        LitemallActivity activity = activityService.queryById(activityId);

        if (activity == null) {
            return;
        }
        if (!activity.getState().equals(LitemallActivity.StateEnum.TO_BE_ISSUE.getCode())) {
            return;
        }

        activity.setState(LitemallActivity.StateEnum.ISSUING.getCode());
        activityService.updateActivityById(activity, null);

        logger.info("系统结束处理延时任务---活动开始---" + this.activityId);
    }
}
